{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction using pairwise-comparison data\n",
    "\n",
    "This notebook provides a gentle introduction to the `choix` library.\n",
    "We consider the case of pairwise-comparison outcomes between items from some set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import choix\n",
    "import networkx as nx\n",
    "import numpy as np\n",
    "\n",
    "%matplotlib inline\n",
    "np.set_printoptions(precision=3, suppress=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In `choix`, items are represented by $n$ consecutive integers $\\{0, \\ldots, n-1 \\}$.\n",
    "The event \"item $i$ wins over item $j$\" is represented by the Python tuple `(i, j)`.\n",
    "Note that the winning item always comes *first* in the tuple.\n",
    "\n",
    "We start by defining a small dataset of comparison outcomes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "n_items = 5\n",
    "data = [\n",
    "    (1, 0), (0, 4), (3, 1),\n",
    "    (0, 2), (2, 4), (4, 3),\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This dataset can be visually represented by using a graph:\n",
    "\n",
    "- each node is an item, and\n",
    "- there is an edge from node $i$ to node $j$ for every observation \"$i$ wins over $j$\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeIAAAFBCAYAAACrYazjAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlcVOX+B/DPiCgzCoqi5a6AoCmICrK4pGYqRCQmWWbi\nen8301yiTG3RboEVqbRo2SJ6b6XXTLPSdiNFlsGFRUVWBc0EBQFlEIHz+8Oc6xFQlpl5Zvm8X69e\nNYczz/nWrfvhe85znkchS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d1Tfylp3\nkwVgpEqFb2Ni4Onpqa/yiIhMHjtiuiMvLy+sjozEBJUKWQ38zs21pldHRjKEiYjugkFMd/WPp5/G\nsshIjFSpsE6hQH17IxUBWKtQYGQdGz4QEVHdeGuaGiwpKQlRERH4bu9eBCsU8NJotPsRq//ejzgw\nIACLli9nJ0xE1EAMYmq0wsJCbI2ORkZqKsqKi2Frbw8XNzfMmDmTE7OIiBqJQUxERCQQnxETEREJ\nxCAmIiISiEFMREQkEIOYiIhIIAYxERGRQAxiIiIigRjEREREAjGIiYiIBGIQExERCcQgJiIiEohB\nTEREJBCDmIiISCAGMRERkUAMYiIiIoEYxERERAIxiImIiARiEBMREQnEICYiIhKIQUxERCQQg5iI\niEggBjEREZFADGIiIiKBGMREREQCMYiJiIgEYhATEREJxCAmIiISiEFMREQkEIOYiIhIIAYxERGR\nQAxiIiIigRjEREREAjGIiYiIBGIQExERCcQgJiIiEohBTEREJBCDmIiISCAGMRERkUAMYiIiIoEY\nxERERAIxiImIiARiEBMREQnEICYiIhKIQUxERCQQg5iIiEggBjEREZFADGIiIiKBGMREREQCMYiJ\niIgEYhATEREJxCAmIiISiEFMREQkEIOYiIhIIAYxERGRQAxiIiIigRjEREREAjGIiYiIBGIQExER\nCcQgJiIiEohBTEREJBCDmIiISCAGMRERkUAMYiIiIoEYxERERAIxiImIiARiEBMREQnEICYiIhLo\n/wGm2bFr6370RQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x10ed31b00>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "graph = nx.DiGraph(data=data)\n",
    "nx.draw(graph, with_labels=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Suppose that we want to fit a [Bradley-Terry model](https://en.wikipedia.org/wiki/Bradley%E2%80%93Terry_model) on this data.\n",
    "`choix` provides several algorithms to do this; below, we use a maximum-likelihood inference algorithm called I-LSR."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.583  0.194 -0.    -0.194 -0.583]\n"
     ]
    }
   ],
   "source": [
    "params = choix.ilsr_pairwise(n_items, data)\n",
    "print(params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The parameters can be thought of as the \"strength\" (or utility) of each item.\n",
    "It is possible to use them to rank the items: simply order the items by increasing parameter value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ranking (worst to best): [4 3 2 1 0]\n"
     ]
    }
   ],
   "source": [
    "print(\"ranking (worst to best):\", np.argsort(params))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is also possible to use the parameters to predict outcomes of future comparisons."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prob(1 wins over 4): 0.69\n"
     ]
    }
   ],
   "source": [
    "prob_1_wins, prob_4_wins = choix.probabilities([1, 4], params)\n",
    "print(\"Prob(1 wins over 4): {:.2f}\".format(prob_1_wins))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dealing with sparsity\n",
    "\n",
    "When the comparison graph is not connected, the maximum-likelihood estimate is not well defined.\n",
    "This happens for example when one item always wins, or always loses.\n",
    "In the following example, item $3$ always wins, and item $0$ always loses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeIAAAFBCAYAAACrYazjAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHYZJREFUeJzt3XtU1XW+//HXTuWWh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a5sLMWAKAzdMTdYLfbtSw7W7dFRamyiz/Tutf0suxsQhgA0AFB3E33z5mjhdnZmhQVpZU2mzp7\nGKlG0rM2myZx4AMA4CwYTZ+n4uJircrK0htvvqmZNpvsTmfbecSOyEi9almaevo8YjphAEBnCOIL\ndOzYMa3LzVVFWZnqa2sVHRur+KQk3ZWWxsIsAMA5EcQAABjEd8QAABhEEAMAYBBBDACAQQQxAAAG\nEcQAABhEEAMAYBBBDACAQQQxAAAGEcQAABhEEAMAYBBBDACAQQQxAAAGEcQAABhEEAMAYBBBDACA\nQQQxAAAGEcQAABhEEAMAYBBBDACAQQQxAAAGEcQAABhEEAMAYBBBDACAQQQxAAAGEcQAABhEEAMA\nYBBBDACAQQQxAAAGEcQAABhEEAMAYBBBDACAQQQxAAAGEcQAABhEEAMAYBBBDACAQf8LEVaKYaHr\nCmgAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x11260a748>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n_items = 4\n",
    "data = [(3, 2), (2, 1), (1, 0)]\n",
    "\n",
    "graph = nx.DiGraph(data=data)\n",
    "nx.draw(graph, with_labels=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In these cases, most of the estimators will fail by default."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/maystre/Documents/choix/choix/utils.py:18: RuntimeWarning: divide by zero encountered in log\n",
      "  params = np.log(weights)\n",
      "/Users/maystre/Documents/choix/choix/utils.py:19: RuntimeWarning: invalid value encountered in subtract\n",
      "  return params - params.mean()\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "Did not converge after 100 iterations",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-8-ad5bf7166344>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mchoix\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0milsr_pairwise\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn_items\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/maystre/Documents/choix/choix/lsr.py\u001b[0m in \u001b[0;36milsr_pairwise\u001b[0;34m(n_items, data, alpha, initial_params, max_iter, tol)\u001b[0m\n\u001b[1;32m    105\u001b[0m     \"\"\"\n\u001b[1;32m    106\u001b[0m     return _ilsr(\n\u001b[0;32m--> 107\u001b[0;31m             n_items, data, alpha, initial_params, max_iter, tol, lsr_pairwise)\n\u001b[0m\u001b[1;32m    108\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    109\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/maystre/Documents/choix/choix/lsr.py\u001b[0m in \u001b[0;36m_ilsr\u001b[0;34m(n_items, data, alpha, params, max_iter, tol, lsr_fun)\u001b[0m\n\u001b[1;32m     30\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mconverged\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     31\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mparams\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 32\u001b[0;31m     \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Did not converge after {} iterations\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmax_iter\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     33\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     34\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mRuntimeError\u001b[0m: Did not converge after 100 iterations"
     ]
    }
   ],
   "source": [
    "choix.ilsr_pairwise(n_items, data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The problem can be solved by adding a little bit of regularization as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-4.159, -0.979,  1.483,  3.655])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choix.ilsr_pairwise(n_items, data, alpha=0.01)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
